﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AntiObesity.Utilities.Wire
{
    public class Table
    {
        public Table()
        {
            Columns = new List<Column>();
            Rows = new List<Row>();
        }

        public List<Column> Columns { get; private set; }
        public List<Row> Rows { get; private set; }

        public void AddColumn(DataType type, string label)
        {
            Columns.Add(new Column { Type = type, Label = label });
        }
        
        public void AddRow(Row row)
        {
            Rows.Add(row);
        }

        public string ToJSON()
        {
            var sb = new StringBuilder();

            sb.Append("table: {");
            // columns:
            sb.Append("cols:[");
            foreach (var col in Columns)
            {
                sb.Append(col.ToJSON());
                if (col != Columns.Last())
                    sb.Append(",");
            }
            sb.Append("],");

            // rows:
            sb.Append("rows:[");
            foreach (var row in Rows)
            {
                sb.Append(row.ToJSON());
                if (row != Rows.Last())
                    sb.Append(",");
            }
            sb.Append("]");


            sb.Append("}");
            return sb.ToString();
        }

    }
}
